//
//  ExpressionNode.m
//  DataCollector
//
//  Created by jiro on 12/06/28.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "ExpressionNode.h"

#import "ValueNode.h"
#import "OperatorNode.h"
#import "UnaryOperatorNode.h"
#import "BinaryOperatorNode.h"

ExpressionNode::ExpressionNode()
{
	operatorNode = NULL;
	rightNode = NULL;
	leftNode = NULL;
}

ExpressionNode::ExpressionNode( const ExpressionNode& node )
{
	operatorNode = new BinaryOperatorNode( static_cast< BinaryOperatorNode::Type >( node.operatorNode->getType() ) );
	rightNode = new ExpressionNode( * node.rightNode );
	leftNode = new ExpressionNode( * node.leftNode );
}


ExpressionNode::ExpressionNode( const ExpressionNode& lhs, const BinaryOperatorNode& ope, const ExpressionNode& rhs )
{
	operatorNode = new BinaryOperatorNode( ope );
	rightNode = new ExpressionNode( rhs );
	leftNode = new ExpressionNode( lhs );
}

ExpressionNode::ExpressionNode( const ExpressionNode& lhs, const UnaryOperatorNode& ope )
{
	operatorNode = new UnaryOperatorNode( ope );
	rightNode = NULL;
	leftNode = new ExpressionNode( lhs );
}

ExpressionNode::~ExpressionNode()
{
	if ( operatorNode != NULL )
	{
		delete operatorNode;
	}
	if ( rightNode != NULL )
	{
		delete rightNode;
	}
	if ( leftNode != NULL )
	{
		delete leftNode;
	}
}

ExpressionNode& ExpressionNode::operator = ( const ExpressionNode& node )
{
	if ( leftNode != NULL )
	{
		leftNode = new ExpressionNode( * node.leftNode );
	}
	if ( rightNode != NULL )
	{
		rightNode = new ExpressionNode( * node.rightNode );
	}
	if ( operatorNode != NULL )
	{
		if ( node.operatorNode->isBinary() )
		{
			operatorNode = new BinaryOperatorNode( static_cast< BinaryOperatorNode::Type >( node.operatorNode->getType() ) );
		}
		else
		{
			operatorNode = new UnaryOperatorNode( static_cast< UnaryOperatorNode::Type >( node.operatorNode->getType() ) );
		}
	}
	return *this;
}

NSString* ExpressionNode::query() const
{
	NSString* s;
	if ( operatorNode->isBinary() )
	{
		s = [ NSString stringWithFormat: @"( %@ %@ %@ )", leftNode->query(), operatorNode->query(), rightNode->query() ];
	}
	else
	{
		s = [ NSString stringWithFormat: @"( %@ ( %@ ) )", operatorNode->query(), leftNode->query() ];
	}
	return s;
}
/*
const ExpressionNode& ExpressionNode::operator + ( const ExpressionNode& rhs ) const
{
}

const ExpressionNode& ExpressionNode::operator - ( const ExpressionNode& rhs ) const
{
}

const ExpressionNode& ExpressionNode::operator * ( const ExpressionNode& rhs ) const
{
}

const ExpressionNode& ExpressionNode::operator / ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator == ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator != ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator <= ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator < ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator >= ( const ExpressionNode& rhs ) const
{
}

bool ExpressionNode::operator > ( const ExpressionNode& rhs ) const
{
}
*/